//
//  HJCustomTabBar.m
//  project
//
//  Created by hu on 2019/4/8.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJCustomTabBar.h"

@implementation HJCustomTabBar

//-(instancetype)initWithFrame:(CGRect)frame {
//    if (self = [super initWithFrame:frame]) {
//        self.frame = frame;
//        [self addSubview:self.bgImage];
//    }
//    return self;
//}

//-(UIImageView *)bgImage {
//    if (!_bgImage) {
//        _bgImage = [[UIImageView alloc]init];
//        _bgImage.contentMode = UIViewContentModeScaleAspectFill;
//        _bgImage.layer.shadowColor = HJColorRGB(0).CGColor;
//        _bgImage.layer.shadowOffset = CGSizeZero;
//        _bgImage.layer.shadowOpacity = 0.2;
//        if (SCREEN_HEIGTH >=812.f) {
//            _bgImage.image = [UIImage imageNamed:@"tabbar"];
//        }else {
//            _bgImage.image = [UIImage imageNamed:@"caidan-bg"];
//        }
////        _bgImage.frame = CGRectMake(0, -20, SCREEN_WIDTH, Height_TabBar+20);
//        _bgImage.frame = CGRectMake(0, 0, SCREEN_WIDTH, Height_TabBar);
//    }
//    return _bgImage;
//}

- (UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *normalImage = [UIImage imageNamed:@"btn1"];
        _addBtn.tag = 100;
        _addBtn.frame = CGRectMake(0, -(49-normalImage.size.height)/2, normalImage.size.width, normalImage.size.height);
        [_addBtn setImage:normalImage forState:UIControlStateNormal];
        //去除选择时高亮
        _addBtn.adjustsImageWhenHighlighted = NO;
        _addBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - normalImage.size.width)/2.0, -(49-normalImage.size.height)/2, normalImage.size.width, normalImage.size.height);
        [_addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_addBtn sizeToFit];
        [self addSubview:_addBtn];
    }
    return _addBtn;
    
}
-(void)addBtnClick {
    if([self.myTabBarDelegate respondsToSelector:@selector(addButtonClick:)]) {
        [self.myTabBarDelegate addButtonClick:self];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat w = self.bounds.size.width;
    CGFloat h = 49;

    CGFloat btnx = 0;
    CGFloat btny = 0;
    //5.0是tabbar中的控件的数量
    CGFloat width = w/5.0;
    CGFloat height = h;
    int i=0;
    for (UIView *btn in self.subviews) {
        if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (i==2) {
                i=3;
            }
            btnx = i*width;
            btn.frame = CGRectMake(btnx, btny, width, height);
            i++;
        }
    }
    //设置自定义button的位置
//    self.addBtn.center = CGPointMake(w*0.5, h*0.5-12);
    self.addBtn.center = CGPointMake(w*0.5, h*0.5);
    [self bringSubviewToFront:self.addBtn];
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.isHidden == NO) {
        //将当前tabbar的触摸点转换坐标系，转换到发布按钮的身上，生成一个新的点
        CGPoint newP = [self convertPoint:point toView:self.addBtn];
        //判断如果这个新的点是在发布按钮身上，那么处理点击事件最合适的view就是发布按钮
        if ( [self.addBtn pointInside:newP withEvent:event]) {
            return self.addBtn;
        }else{//如果点不在发布按钮身上，直接让系统处理就可以了
            return [super hitTest:point withEvent:event];
        }
    } else {//tabbar隐藏了，那么说明已经push到其他的页面了，这个时候还是让系统去判断最合适的view处理就好了
        return [super hitTest:point withEvent:event];
    }
}
@end
